<template>
  <div class="app-container1 show-back-class">
    <div style="width: 100%;height: 60px;border-bottom: #1C97FF 1px solid;padding:  10px 20px;display: flex;">
      <img style="width: 340px; height: 40px; margin-right: 20px;" src="@/assets/bigScreen/index/LOGO.jpg" alt="">
      <big-tab-Bar />
    </div>
    <div class="content-bottom">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="中文名称" prop="chineseName">
          <el-input v-model="queryParams.chineseName" placeholder="请输入中文名称"
                    clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入编码" clearable
            size="small"  @keyup.enter.native="handleQuery"  />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
            <el-option class="index-option" v-for="dict in $store.state.factorDict.statusOptions"
                       :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini"
                     @click="handleAdd" v-hasPermi="['factor:productCategorize:add']" >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-refresh" size="mini" @click="handleClearCache"
                     v-hasPermi="['factor:productCategorize:clear']">清理缓存</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>

      <el-table  border v-loading="loading" :height="tableHeight" :data="productCategorizeList" row-key="code_key" lazy  :load="load"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="序号" align="center" type="index" width="80" />
        <el-table-column label="中文名称" align="left" prop="chineseName"  />
        <el-table-column label="编码" align="center" prop="code" />
        <el-table-column label="分类级别" align="center" prop="level" width="100">
          <template slot-scope="scope">
            {{selectDictLabelDefault($store.state.factorDict.productCategorizeOptions, scope.row.level)}}
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="orderNum" width="100"/>
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template slot-scope="scope">
            {{selectDictLabelDefault($store.state.factorDict.statusOptions, scope.row.status)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button size="mini"  type="text" icon="el-icon-edit"  @click="handleUpdate(scope.row)"
              v-hasPermi="['factor:productCategorize:edit']" >修改</el-button>
            <el-button size="mini"  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['factor:productCategorize:remove']" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <change-form ref="changeFormRef" @getList="getList" />
    </div>
  </div>
</template>

<script>
import { listProductCategorize, getProductCategorize, delProductCategorize, addProductCategorize, updateProductCategorize, clearProductCategorizeCache } from "@/api/factor/productCategorize";
import ChangeForm from "./changeForm"
import store from "../../../store";
import bigTabBar from '@/views/components/bigTabBar/index'
export default {
  name: "ProductCategorize",
  components:{
    ChangeForm,bigTabBar
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 产品分类表格数据
      productCategorizeList: [],
      // 查询参数
      queryParams: {
        code: null,
        parentCode: null,
        ancestors: null,
        chineseName: null,
        level: null,
        status: null,
        orderNum: null,
      },
      tableHeight: '',
    };
  },
  created() {
    this.tableHeight = window.innerHeight - 220;
    this.getList();
  },
  methods: {
    /** 查询产品分类列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.chineseName||this.queryParams.code||this.queryParams.status){
        this.queryParams.parentCode = null
      }else {
        this.queryParams.parentCode = "0";
      }
      listProductCategorize(this.queryParams).then(response => {
        response.rows.forEach((item, i) => {
          item['code_key']=item['level']+"_"+item['code'];
          item.hasChildren = item['level']<3
        });
        this.productCategorizeList = response.rows;
        // this.productCategorizeList =  this.genTreeShowTable(response.rows);
        this.loading = false;
      });
    },
    load(tree, treeNode, resolve) {
      listProductCategorize({parentCode: tree.code,level:tree.level+1}).then(res => {
        res.rows.forEach((item, i) => {
          item['code_key']=item['level']+"_"+item['code'];
          item.hasChildren = item['level']<3
        });
        resolve(res.rows)
      });
    },
    genTreeShowTable(list){
      let p0 = [],p1=[],p2=[],p3=[];
      for (let item of list){
        item['code_key']=item['level']+"_"+item['code'];
        if(item.level==0){
          p0.push(item);
        }else if(item.level==1){
          p1.push(item);
        }else if(item.level==2){
          p2.push(item);
        }else if(item.level==3){
          p3.push(item);
        }
      }
      for (let father of p2) {
        father['children'] = p3.filter(child => { return father['code'] === child['parentCode']});
      }
      for (let father of p1) {
        father['children'] = p2.filter(child => { return father['code'] === child['parentCode']});
      }
      for (let father of p0) {
        father['children'] = p1.filter(child => { return father['code'] === child['parentCode']});
      }
      if(p0.length>0){
        return p0;
      }
      if(p1.length>0){
        return p1;
      }
      if(p2.length>0){
        return p2;
      }
      return p3;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$store.dispatch('SelectProductCategorizeDict');
      this.$refs.changeFormRef.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$store.dispatch('SelectProductCategorizeDict');
      this.$refs.changeFormRef.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductCategorize(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    handleClearCache(){
      clearProductCategorizeCache().then(response => {
        if (response.code === '200') {
          this.msgSuccess("清理成功");
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container1{
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #000D17;
  // background: url("../../assets/bg2.jpg") no-repeat;
  // background-size: 100% 100%;
}
.content-bottom{
  width: 100%;
  height: calc(100% - 60px);
  background-color: #000D17;
  padding: 20px;
}
</style>